package com.random.sort;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 随机分组
 */
public class RandomGroup {

    public static void main(String[] args) {
        String[] team = {"a", "b", "c", "d", "e", "f", "g", "h"};
        String[][] result = randomGroup(team, 4);
        for (String[] strings : result) {
            System.out.println(Arrays.toString(strings));
        }
    }

    public static String[][] randomGroup(String[] team, int num) {
        int col = team.length / num;
        int ys = team.length % num;
        if (ys > 0){
            col = col + 1;
        }
        int a = 0, b = 0;
        String[][] result = new String[num][col];
        while (team.length > 0) {
            int index = (int) (Math.random() * team.length);
            result[a][b] = team[index];
            team[index] = team[team.length - 1];
            team = Arrays.copyOf(team, team.length - 1);
            if (a < num - 1) {
                a++;
            } else if (b < col - 1) {
                a = 0;
                b++;
            }
        }
        return result;
    }

    public List<String> getRandomGroup() {

        TwoList twoList = new TwoList();
        List<List> lists = twoList.bgList();
        List<String> boyList = lists.get(0);
        List<String> girlList = lists.get(1);
        System.out.println("分组前boyList：" + boyList);
        System.out.println("分组前girlList:" + girlList);

        // List转数组
        String[] boyArr = new String[boyList.size()];
        String[] girlArr = new String[girlList.size()];

        String[] boyArray = boyList.toArray(boyArr);
        boyList.toArray(boyArr);
        String[][] boyResult = randomGroup(boyArray, 4);

        String[] girlArray = girlList.toArray(girlArr);
        girlList.toArray(girlArr);
        String[][] girlResults = randomGroup(girlArray, 4);

        // 数组转List
        List<String> borObjects1 = new ArrayList<String>();
        List<String> girlObjects2 = new ArrayList<String>();
        for (String[] strings : boyResult) {
            String str1 = Arrays.toString(strings);
            borObjects1.add(str1);
        }
        for (String[] strings : girlResults) {
            String str2 = Arrays.toString(strings);
            girlObjects2.add(str2);
        }

        // 分组组合
        List<String> objects3 = new ArrayList<String>();
        for (int s = 0; s < borObjects1.size(); s++) {
            objects3.add(borObjects1.get(s).substring(0, borObjects1.get(s).length() - 1) + "，" + girlObjects2.get(s).substring(1, girlObjects2.get(s).length()));
        }
        return objects3;
    }
}